package com.liuhung.ash.fang.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liuhung.base.web.BaseAction;
import com.liuhung.ash.fang.entity.Youhui;
import com.liuhung.ash.fang.page.YouhuiPage;
import com.liuhung.ash.fang.service.YouhuiService;
 
/**
 *	@ClassName: YouhuiDao 
 *  @Description: <b>优惠restful 數據接口</b>
 *  @author 刘  鸿 
 *  @date 2015-10-6 20:46:01
 */ 
@Controller
@RequestMapping("/r/youhui") 
public class RestYouhuiController extends BaseAction{
	
	private final static Logger log= Logger.getLogger(RestYouhuiController.class);
	
	
	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private YouhuiService youhuiService; 
	
	@RequestMapping
	@ResponseBody
	public List<Youhui> restfulList(YouhuiPage page,HttpServletRequest request)throws Exception{
		page.setNum( youhuiService.queryByCount(page));
		List<Youhui> youhuis = youhuiService.queryByList(page);
		return youhuis;
	}
	@RequestMapping("/{id}")
	@ResponseBody
	public Youhui restfulGetById(@PathVariable Object id,HttpServletRequest request)throws Exception{
		Youhui youhui = youhuiService.queryById(id);
		return youhui;
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public void restfulSave(Youhui entity,HttpServletRequest request,HttpServletResponse response){
		try{
				if(entity.getId()==null||StringUtils.isBlank(entity.getId().toString())){
					youhuiService.add(entity);
				}else{
					youhuiService.update(entity);
				}
				sendSuccessMessage(response, "优惠保存成功!");
			}catch(Exception e){
				sendFailureMessage(response, "优惠保存失败!");
			}
	}
	@RequestMapping(method=RequestMethod.DELETE)
	public void restfulDelete(String[] id,HttpServletResponse response) throws Exception{
		try{
			youhuiService.delete(id);
			sendSuccessMessage(response, "删除成功");
		}catch(Exception e){
			sendFailureMessage(response, "优惠删除失败!");
		}
	}
}
